home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cgazv5n4.arc / STRINGC.H < prev    next >
C/C++ Source or Header  |  1991-09-23  |  2KB  |  52 lines

  1. //--- STRINGC.H -------------------------- Listing 1 -----------
  2. // String class designed for editing
  3. // by Bruce Eckel
  4. //
  5. // (c) 1991 C Gazette: Object code may be used freely. Source
  6. // code may be used as long as authorship and publication are
  7. // acknowledged.                    Verified for Borland C++.
  8. //--------------------------------------------------------------
  9. #ifndef STRINGC_H_
  10. #define STRINGC_H_
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14.  
  15. class string {
  16.   struct piece {  // private nested structure
  17.     char* str;
  18.     piece* next;
  19.     int plength;  // length of this piece
  20.     piece(piece* nxt, char * s) : next(nxt) {
  21.       plength = strlen(s);
  22.       str = new char[plength + 1];
  23.       strcpy(str, s);
  24.     }
  25.     // break a piece in two by creating a new piece:
  26.     void break_piece(int size) {
  27.       // insert a piece in the list; start the string at 'size':
  28.       next = new piece(next, str + size);
  29.       // truncate the original string at 'size':
  30.       str[size] = '\0';
  31.       plength = size;
  32.     }
  33.     ~piece() { delete str; }
  34.     void print() { printf("%s", str); }
  35.   };
  36.   piece * head, * cursor;
  37.   int Length;
  38.   void erase_string();  // used in more than one place
  39.   void defragment();
  40. public:
  41.   string(char * s);
  42.   void append(char * s);
  43.   void prepend(char* s);
  44.   void insert(int position, char* s);
  45.   void erase(int position, int count = 1);
  46.   int search(char * findstring);  // returns position or -1
  47.   int length() { return Length; }
  48.   void print();
  49.   ~string() {  erase_string(); }
  50. };
  51.  
  52. #endif // STRINGC_H_